<template>
  <div class="login">
    <div class="login-wrap">
      <img class="login-bg1" src="@/assets/common/login_bg1.png" alt="">
      <img class="login-bg2" src="@/assets/common/login_bg2.png" alt="">
      <img class="login-bg3" src="@/assets/common/login_bg3.png" alt="">
      <div class="login-form-wrap">
        <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="login-form">
          <h3 class="title">欢迎注册</h3>
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>

          <el-form-item prop="email">
            <el-input
              v-model="registerForm.email"
              type="email"
              auto-complete="off"
              placeholder="邮箱"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="email" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="toRegister"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item style="width: 100%">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width: 100%"
              @click.native.prevent="toRegister"
            >
              <span v-if="!loading">注 册</span>
              <span v-else>注 册 中...</span>
            </el-button>
          </el-form-item>
          <div class="tips">
            <div class="row mb-3">
              <el-button
                type="text"
                class="goLogin"
                @click="$router.push('/login')"
              >已有账号，去登录</el-button>
            </div>
          </div>
        </el-form>
      </div>
    </div>

    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2021 LinkWeChat All Rights Reserved.</span>
    </div>

  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      registerForm: {
        username: '',
        password: '',
        email: ''
      },
      registerRules: {
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        email: [
          {
            pattern:
              /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/,
            required: true,
            trigger: 'blur',
            message: '请输入正确的邮箱'
          }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async toRegister() {
      await this.$refs.registerForm.validate()
      await register(this.registerForm)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .el-form-item__error {
    color: #fff
  }
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  // background-image: url('../assets/image/login-background.png');
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #027dfe;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  position: relative;
  top: 160px;
  left: 30px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  // color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  z-index: 90;
}
.login-code-img {
  height: 38px;
}
.login ::v-deep.el-dialog {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin-top: 0vh !important;
}
.el-dialog__body {
  padding: 0px 20px;
}
[class*='login-bg'] {
  position: absolute;
  z-index: -1;
}
.login-form-wrap {
  position: relative;
  background: #fff;
  height: 609px;
  top: 50px;
  left: 100px;
}
.login-wrap {
  position: relative;
  width: 1180px;
  height: 720px;
}
.login-bg1 {
  top: 0;
  left: 0;
}
.login-bg2 {
  top: 50px;
  left: 370px;
  z-index: 10;
}
.login-bg3 {
  top: 220px;
  right: 25px;
  z-index: 20;
}
.goLogin{
  color: #333;
  :hover{
    color: #027dfe;
  }
}

</style>
